package com.IOT_LSL;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;

public class Fragment_control extends Fragment implements View.OnTouchListener {
    private Switch edit_mode;
    private SeekBar seekBar;
    private TextView fragment_ctl_message;
    private boolean mode_is_edit=false;
    private Button btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9;
    private SharedPreferences sp;

    View view;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.activity_control,container,false);
        sp = getContext().getSharedPreferences("btn_set_data", Context.MODE_PRIVATE);
        InitView();
        return view;
    }

    private void InitView() {
        initwidget();
        edit_mode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    mode_is_edit = true;
                    Toast.makeText(getActivity(),"编辑模式开启",Toast.LENGTH_SHORT).show();
                }
                else{
                    mode_is_edit = false;
                    Toast.makeText(getActivity(),"编辑模式关闭",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void seekBar_listener() {
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                OutputStream tcp_outputStream = ((MainActivity)getActivity()).get_tcp_outputStream();
                if(tcp_outputStream != null){
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            String tcp_send_data = "=G0"+String.valueOf(progress).trim()+"00";
                            try {
                                if (tcp_send_data.equals("")) {
                                    Log.i("TCP", "输入不能为空");
                                } else {
                                    if (tcp_outputStream != null) {
                                        byte[] sendData = tcp_send_data.getBytes(Charset.forName("ASCII"));
                                        tcp_outputStream.write(sendData, 0, sendData.length);
                                        tcp_outputStream.flush();
                                    }
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }).start();
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        if( mode_is_edit && action == MotionEvent.ACTION_DOWN ){
            Dialog_Set_Btn dialog_set_btn = new Dialog_Set_Btn(getActivity(), v.getId());
            dialog_set_btn.show();
        }else if( !mode_is_edit ){
            OutputStream tcp_outputStream = ((MainActivity)getActivity()).get_tcp_outputStream();
            if(tcp_outputStream != null){
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String tcp_send_data = null;
                        if( action == MotionEvent.ACTION_DOWN ){
                            tcp_send_data = sp.getString("btn"+v.getId()+"_down_data","").trim();
                        } else if( action == MotionEvent.ACTION_UP ){
                            tcp_send_data = sp.getString("btn"+v.getId()+"_up_data","").trim();
                        }
                        try {
                            if (tcp_send_data.equals("")) {
                                Log.i("TCP", "输入不能为空");
                            } else {
                                //注意charset.forName 字符编码，utf-8中文。。。。。
                                if (tcp_outputStream != null) {
                                    byte[] sendData = tcp_send_data.getBytes(Charset.forName("ASCII"));
                                    tcp_outputStream.write(sendData, 0, sendData.length);
                                    tcp_outputStream.flush();
                                }
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        }
        return false;
    }

    public void initwidget(){
        edit_mode = view.findViewById(R.id.edit_mode);
        fragment_ctl_message = view.findViewById(R.id.fragment_ctl_message);
        seekBar = view.findViewById(R.id.seekBar);
        seekBar.setMax(255);
        seekBar.setProgress(10);
        seekBar_listener();
        btn_1 = view.findViewById(R.id.fragment_ctl_btn_1);
        btn_2 = view.findViewById(R.id.fragment_ctl_btn_2);
        btn_3 = view.findViewById(R.id.fragment_ctl_btn_3);
        btn_4 = view.findViewById(R.id.fragment_ctl_btn_4);
        btn_5 = view.findViewById(R.id.fragment_ctl_btn_5);
        btn_6 = view.findViewById(R.id.fragment_ctl_btn_6);
        btn_7 = view.findViewById(R.id.fragment_ctl_btn_7);
        btn_8 = view.findViewById(R.id.fragment_ctl_btn_8);
        btn_9 = view.findViewById(R.id.fragment_ctl_btn_9);
        btn_1.setOnTouchListener(this);
        btn_2.setOnTouchListener(this);
        btn_3.setOnTouchListener(this);
        btn_4.setOnTouchListener(this);
        btn_5.setOnTouchListener(this);
        btn_6.setOnTouchListener(this);
        btn_7.setOnTouchListener(this);
        btn_8.setOnTouchListener(this);
        btn_9.setOnTouchListener(this);
        btn_1.setText(sp.getString("btn"+btn_1.getId()+"_name","Button1"));
        btn_2.setText(sp.getString("btn"+btn_2.getId()+"_name","Button2"));
        btn_3.setText(sp.getString("btn"+btn_3.getId()+"_name","Button3"));
        btn_4.setText(sp.getString("btn"+btn_4.getId()+"_name","Button4"));
        btn_5.setText(sp.getString("btn"+btn_5.getId()+"_name","Button5"));
        btn_6.setText(sp.getString("btn"+btn_6.getId()+"_name","Button6"));
        btn_7.setText(sp.getString("btn"+btn_7.getId()+"_name","Button7"));
        btn_8.setText(sp.getString("btn"+btn_8.getId()+"_name","Button8"));
        btn_9.setText(sp.getString("btn"+btn_9.getId()+"_name","Button9"));
    }
}
